/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import accessor.Customer;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author QUYET
 */
@WebServlet(name = "changepass", urlPatterns = {"/changepass"})
public class changepass extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            if (request.getSession().getAttribute("customer")==null) {
                response.sendRedirect("home/index.jsp");
            }
            if (request.getParameter("change_pass") != null) {
                Customer customer = (Customer) request.getSession().getAttribute("customer");
                String old_password = request.getParameter("old_password");
                String new_password = request.getParameter("new_password");
                String conf_new_pass = request.getParameter("conf_new_pass");
                List<String> errorList = new ArrayList<String>();
                if (!old_password.equals("") && !new_password.equals("") && !conf_new_pass.equals("") && conf_new_pass.equals(new_password)) {
                    String originalPass = customer.getPassword();
                    if (!old_password.equals(originalPass)) {
                        errorList.add("<p>Your old password does not match your previous password!</p>");
                        response.sendRedirect("home/change_pass.jsp");
                    }
                        
                } else {
                    if (old_password.equals("")) {
                        errorList.add("<p>You must enter your old password!</p>");
                    }
                    if (new_password.equals("")) {
                        errorList.add("<p>You must enter your new password!</p>");
                    }
                    if (conf_new_pass.equals("")) {
                        errorList.add("<p>You must enter your new confirmed password!</p>");
                    }
                    if (!conf_new_pass.equals(new_password)) {
                        errorList.add("<p>Confirmed new password does not match new password!</p>");
                    }
                    request.getSession().setAttribute("changePassError", errorList);
                    response.sendRedirect("home/change_pass.jsp");
                }
            } else {
                session.removeAttribute("changePassError");
                response.sendRedirect("home/change_pass.jsp");
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
